package lex.proxyverfier;

import javax.swing.ActionMap;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MainMenuBar extends JMenuBar {
	private JMenu fileMenu;
	private JMenuItem exitMenuItem;
	private JMenu importMenu;
	private JMenu exportMenu;
	private JMenu helpMenu;
	private JMenu editMenu;
	private ActionMap am = null;

	public MainMenuBar() {
		super();
		this.am = ProxyVerifier.getApplication().getContext().getActionManager().getActionMap();
		this.initGUI();
	}

	private void initGUI() {
		try {
			this.add(getFileMenu());
			this.add(getEditMenu());
			this.add(getHelpMenu());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(getImportMenu());
			fileMenu.add(getExportMenu());
			fileMenu.addSeparator();
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	private JMenu getEditMenu() {
		if (editMenu == null) {
			editMenu = new JMenu();
			editMenu.setText("Edit");
			editMenu.add(am.get("add"));
			editMenu.add(am.get("remove"));
			editMenu.addSeparator();
			editMenu.add(am.get("preferences"));
		}
		return editMenu;
	}

	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
		}
		return exitMenuItem;
	}

	public JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.add(am.get("about"));
		}
		return helpMenu;
	}

	private JMenu getImportMenu() {
		if (importMenu == null) {
			importMenu = new JMenu();
			importMenu.setText("Import");
			importMenu.add(am.get("importPlain"));
		}
		return importMenu;
	}

	private JMenu getExportMenu() {
		if (exportMenu == null) {
			exportMenu = new JMenu();
			exportMenu.setText("Export");
			exportMenu.add(am.get("exportPlain"));
		}
		return exportMenu;
	}
}
